import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:sph_shipper/AppConfig/Config.dart';

class PaymentCycleSheet extends StatefulWidget {
  const PaymentCycleSheet({super.key, required this.payPeriod, required this.payPeriodStr, required this.dayStr});
  final int payPeriod;
  final String payPeriodStr;
  final String dayStr;
  @override
  State<PaymentCycleSheet> createState() => _PaymentCycleSheetState();
}

class _PaymentCycleSheetState extends State<PaymentCycleSheet> {
  late TextEditingController _periodText = TextEditingController(text: widget.dayStr);
  String dayStr="";
  int payPeriod=-1;
  String payTitle="";
  bool isNumeric(String str) {
    if (str == null || str.isEmpty) {
      return false;
    }
    final numericRegex = RegExp(r'^[0-9]+$');
    return numericRegex.hasMatch(str);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    dayStr=widget.dayStr;
    payPeriod=widget.payPeriod;
    payTitle=widget.payPeriodStr;
  }
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Container(
      padding: EdgeInsets.only(left: 10.w,right: 10.w,top: 10.h),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10))),
      child: Column(children: [
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              TextButton(onPressed: (){Navigator.pop(context);}, child: Text("取消",style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),)),
              Text("选择付款周期",style: TextStyle(color: MyColors.textColor,fontSize: 16.sp,fontWeight: FontWeight.w600),),
              TextButton(onPressed: (){
                if(payPeriod==-1&&dayStr==""){
                  BrnToast.show("请选择支付周期", context);
                }else if((payPeriod==-1&&dayStr!=""&&!isNumeric(dayStr))||(payPeriod==-1&&dayStr!=""&&isNumeric(dayStr)&&int.parse(dayStr)>365)){
                  BrnToast.show("请填写正确的天数", context);
                }else{
                  Navigator.pop(context,[payPeriod,dayStr,payTitle]);
                }

              }, child: Text("确定",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),)),
            ],
          ),
          Divider(height: 16.h,color: MyColors.backgreyColor,),
          SizedBox(height: 8.h,),
          Container(
            margin: EdgeInsets.only(left: 10.w,right: 10.w),
            child:  Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                GestureDetector(
                  child:  BrnTagCustom(
                    tagText: "货到付",
                    backgroundColor: payPeriod==801?Color.fromRGBO(251, 236, 228, 1):MyColors.backgreyColor,
                    textColor: payPeriod==801?MyColors.themeColor:Colors.black,
                    fontSize: 14.sp,
                    textPadding:EdgeInsets.only(left: 8.w,right: 8.w,top: 4.h,bottom: 4.h),
                  ),
                  onTap: (){
                      setState(() {
                        payPeriod=801;
                        payTitle="货到付";
                        dayStr="";
                      });
                  },
                ),
                GestureDetector(
                  child:  BrnTagCustom(
                    tagText: "回单付",
                    backgroundColor: payPeriod==802?Color.fromRGBO(251, 236, 228, 1):MyColors.backgreyColor,
                    textColor: payPeriod==802?MyColors.themeColor:Colors.black,
                    fontSize: 14.sp,
                    textPadding:EdgeInsets.only(left: 8.w,right: 8.w,top: 4.h,bottom: 4.h),
                  ),
                  onTap: (){
                    setState(() {
                      payPeriod=802;
                      payTitle="回单付";
                      dayStr="";
                    });
                  },
                ),
                GestureDetector(
                  child:  BrnTagCustom(
                    tagText: "月   结",
                    backgroundColor: payPeriod==830?Color.fromRGBO(251, 236, 228, 1):MyColors.backgreyColor,
                    textColor: payPeriod==830?MyColors.themeColor:Colors.black,
                    fontSize: 14.sp,
                    textPadding:EdgeInsets.only(left: 8.w,right: 8.w,top: 4.h,bottom: 4.h),
                  ),
                  onTap: (){
                    setState(() {
                      payPeriod=830;
                      payTitle="月结";
                      dayStr="";
                    });
                  },
                ),
                GestureDetector(
                  child:  BrnTagCustom(
                    tagText: "季   结",
                    backgroundColor: payPeriod==890?Color.fromRGBO(251, 236, 228, 1):MyColors.backgreyColor,
                    textColor: payPeriod==890?MyColors.themeColor:Colors.black,
                    fontSize: 14.sp,
                    textPadding:EdgeInsets.only(left: 8.w,right: 8.w,top: 4.h,bottom: 4.h),
                  ),
                  onTap: (){
                    setState(() {
                      payPeriod=890;
                      payTitle="季结";
                      dayStr="";
                    });
                  },
                ),

              ],
            ),
          ),

        SizedBox(height: 8.h,),
        Container(
          margin: EdgeInsets.only(left: 10.w,right: 10.w),
          child:   Row(

            children: [
              SizedBox(),
              GestureDetector(
                child:  BrnTagCustom(
                  tagText: "半年结",
                  backgroundColor: payPeriod==980?Color.fromRGBO(251, 236, 228, 1):MyColors.backgreyColor,
                  textColor: payPeriod==980?MyColors.themeColor:Colors.black,
                  fontSize: 14.sp,
                  textPadding:EdgeInsets.only(left: 8.w,right: 8.w,top: 4.h,bottom: 4.h),
                ),
                onTap: (){
                  setState(() {
                    payPeriod=980;
                    payTitle="半年结";
                    dayStr="";
                  });
                },
              ),

            ],
          ),
        ),
        Divider(height: 10.h,color: MyColors.backgreyColor,),
        Container(
          margin: EdgeInsets.only(left: 10.w,right: 10.w),

          child: Row(
            children: [
              Text("货到",style:TextStyle(color: Colors.black,fontSize: 14.sp) ,),
              Container(
                width: 100.w,
                height: 35.h,
                child: TextField(
                  controller: _periodText,
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    hintText:'输入天数',
                    hintStyle:TextStyle(fontSize: 14.sp,color: MyColors.hintColor),
                    filled: true,
                    fillColor: MyColors.backgreyColor,
                    // border: InputBorder.none,
                    alignLabelWithHint: true,
                    // contentPadding: EdgeInsets.symmetric(vertical: 6.0.h, horizontal: 0.0.w),
                  ),
                  keyboardType: TextInputType.number,
                  textAlign: TextAlign.left,
                  style: TextStyle(color: Colors.black),
                  onChanged: (value) {
                    setState(() {
                      dayStr=value;
                      if(dayStr.length>0)
                        payPeriod=-1;
                        payTitle="";
                    });
                  },
                ),
              ),
              Text("天后付（ 最多365天）",style:TextStyle(color: Colors.black,fontSize: 14.sp) ,),
            ],
          ),
        ),


      ],),
    );
  }
}
